﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;

using ProjectBase.Core;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;
using Survey.Common;

namespace Survey.Filters
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class ModelManagerFilterAttribute : ActionFilterAttribute, IPublishingReporter
    {
        protected const string CssContent = " {0}Controller {1}Content ";

        #region OnAction
        /// <summary>
        /// Sets the Current Action, Controller name
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            this.PublishInfo(() => "OnActionExecuting--started-ModelManagerFilter");
            var controller = filterContext.Controller as IWebController<IViewModel>;
            if (controller.Is())
            {
                FillModel(filterContext, controller);
            }
            this.PublishInfo(() => "OnActionExecuting-finished-ModelManagerFilter");
        }
        #endregion OnAction

        #region protected FillModel
        protected virtual void FillModel(ActionExecutingContext filterContext, IWebController<IViewModel> controller)
        {
            controller.Model.MasterModel.CurrentAction = filterContext.ActionDescriptor.ActionName;
            controller.Model.MasterModel.ControllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            controller.Model.MasterBodyCssClass = CssContent.FormatWith(
                filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower(),
                filterContext.ActionDescriptor.ActionName.ToLower());
        }
        #endregion protected FillModel
    }
}
